package com.libl.advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

//环绕增强，注意导入的包org.aopalliance.intercept.MethodInterceptor
public class MyAroundAdvice implements MethodInterceptor {
    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        //环绕增强必须要调用目标对象中的原始方法，否则不会执行
        try {
            System.out.println("前置增强");

            //必须调用原始业务代码，返回值是原始方法的返回值，如果原始方法返回值为void则返回null
            Object result = methodInvocation.proceed();

            System.out.println("后置增强");

            //必须return返回值，否则原始方法会一直结束不了
            return result;

        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("异常增强");
        } finally {
            System.out.println("最终增强");
        }
        return null;
    }
}
